{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## PyTorch练习\n",
    "## 识别手写网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "## 加载数据\n",
    "from torchvision import datasets,transforms\n",
    "transform = transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.1307,),(0.3081,))\n",
    "])\n",
    "\n",
    "trainset = datasets.MNIST('data',train=True,download=False,transform=transform)\n",
    "testset = datasets.MNIST('data',train=False,download=False,transform=transform)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T11:33:11.388750900Z",
     "start_time": "2023-10-10T11:32:39.907417500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         ...,\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0]],\n\n        [[0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         ...,\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0]],\n\n        [[0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         ...,\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0]],\n\n        [[0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         ...,\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0]],\n\n        [[0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         ...,\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0],\n         [0, 0, 0,  ..., 0, 0, 0]]], dtype=torch.uint8)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainset.data[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T11:33:38.217797200Z",
     "start_time": "2023-10-10T11:33:37.531829500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "## 定义神经网络模型\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "class LeNet(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(LeNet,self).__init__()\n",
    "        self.c1 = nn.Conv2d(1,6,5)\n",
    "        self.c3 = nn.Conv2d(6,16,5)\n",
    "        self.fc1 = nn.Linear(16*4*4,120)\n",
    "        self.fc2 = nn.Linear(120,84)\n",
    "        self.fc3 = nn.Linear(84,10)\n",
    "\n",
    "    def num_flat_features(self,x):\n",
    "        size = x.size()[1:]\n",
    "        num_features = 1\n",
    "        for s in size:\n",
    "            num_features *=s\n",
    "        return num_features\n",
    "\n",
    "    def forward(self,x):\n",
    "        x = F.max_pool2d(F.relu(self.c1(x)),2) # 池化核为n*n\n",
    "        x = F.max_pool2d(F.relu(self.c3(x)),2)\n",
    "        x = x.view(-1,self.num_flat_features(x))\n",
    "        x = F.relu(self.fc1(x))\n",
    "        x = F.relu(self.fc2(x))\n",
    "        x = self.fc3(x)\n",
    "        return x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T11:51:46.822938700Z",
     "start_time": "2023-10-10T11:51:46.794942700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "import torch\n",
    "CUDA = torch.cuda.is_available()\n",
    "if CUDA:\n",
    "    lenet = LeNet().cuda()\n",
    "else:\n",
    "    lenet = LeNet()\n",
    "from torch import optim\n",
    "criterion = nn.CrossEntropyLoss()\n",
    "optimizer = optim.SGD(lenet.parameters(),lr=0.001,momentum=0.9)\n",
    "# 使用PyTorch自带的数据加载工具DataLoader来加载训练数据\n",
    "trainloader = torch.utils.data.DataLoader(trainset,batch_size=4,shuffle=True,num_workers=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T11:56:28.523091300Z",
     "start_time": "2023-10-10T11:56:25.872941600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Epoch:1,Batch: 1000] Loss:1.448\n",
      "[Epoch:1,Batch: 2000] Loss:0.309\n",
      "[Epoch:1,Batch: 3000] Loss:0.211\n",
      "[Epoch:1,Batch: 4000] Loss:0.183\n",
      "[Epoch:1,Batch: 5000] Loss:0.153\n",
      "[Epoch:1,Batch: 6000] Loss:0.146\n",
      "[Epoch:1,Batch: 7000] Loss:0.106\n",
      "[Epoch:1,Batch: 8000] Loss:0.108\n",
      "[Epoch:1,Batch: 9000] Loss:0.098\n",
      "[Epoch:1,Batch:10000] Loss:0.105\n",
      "[Epoch:1,Batch:11000] Loss:0.091\n",
      "[Epoch:1,Batch:12000] Loss:0.076\n",
      "[Epoch:1,Batch:13000] Loss:0.093\n",
      "[Epoch:1,Batch:14000] Loss:0.087\n",
      "[Epoch:1,Batch:15000] Loss:0.086\n",
      "[Epoch:2,Batch: 1000] Loss:0.077\n",
      "[Epoch:2,Batch: 2000] Loss:0.058\n",
      "[Epoch:2,Batch: 3000] Loss:0.061\n",
      "[Epoch:2,Batch: 4000] Loss:0.062\n",
      "[Epoch:2,Batch: 5000] Loss:0.054\n",
      "[Epoch:2,Batch: 6000] Loss:0.077\n",
      "[Epoch:2,Batch: 7000] Loss:0.065\n",
      "[Epoch:2,Batch: 8000] Loss:0.062\n",
      "[Epoch:2,Batch: 9000] Loss:0.052\n",
      "[Epoch:2,Batch:10000] Loss:0.070\n",
      "[Epoch:2,Batch:11000] Loss:0.068\n",
      "[Epoch:2,Batch:12000] Loss:0.050\n",
      "[Epoch:2,Batch:13000] Loss:0.056\n",
      "[Epoch:2,Batch:14000] Loss:0.063\n",
      "[Epoch:2,Batch:15000] Loss:0.054\n",
      "训练结束\n"
     ]
    }
   ],
   "source": [
    "# 训练数据\n",
    "def train(model,criterion,optimizer,epochs=1):\n",
    "    for epoch in range(epochs):\n",
    "        running_loss = 0.0\n",
    "        for i,data in enumerate(trainloader,0):\n",
    "            inputs,labels=data\n",
    "            if CUDA:\n",
    "                inputs,labels = inputs.cuda(),labels.cuda()\n",
    "            optimizer.zero_grad()\n",
    "            outputs = model(inputs)\n",
    "            loss = criterion(outputs,labels)\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "\n",
    "            running_loss += loss.item()\n",
    "            if i%1000 ==999:\n",
    "                print('[Epoch:%d,Batch:%5d] Loss:%.3f' %(epoch+1,i+1,running_loss/1000))\n",
    "                running_loss = 0.0\n",
    "    print('训练结束')\n",
    "\n",
    "train(lenet,criterion,optimizer,epochs=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T12:09:18.684705400Z",
     "start_time": "2023-10-10T12:06:19.089977200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "testloader = torch.utils.data.DataLoader(testset,batch_size=4,shuffle=False,num_workers=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T12:15:31.764044600Z",
     "start_time": "2023-10-10T12:15:31.742730500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:98 %\n"
     ]
    }
   ],
   "source": [
    "def test(testloader,model):\n",
    "    correct =0\n",
    "    total = 0\n",
    "    for data in testloader:\n",
    "        images,labels = data\n",
    "        if CUDA:\n",
    "            images = images.cuda()\n",
    "            labels = labels.cuda()\n",
    "        outputs = model(images)\n",
    "        _,pred = torch.max(outputs,1)\n",
    "        total+=labels.size(0)\n",
    "        correct+=(pred==labels).sum()\n",
    "    print(\"测试集精度:%d %%\"%(100*correct/total))\n",
    "\n",
    "test(testloader,lenet)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T12:20:39.244546Z",
     "start_time": "2023-10-10T12:20:30.078515200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
